var startDate=echartUtil.GetDateTimeStr(0); 

$(function(){
	page.bind();
	page.initSelect(1); 
	page.getCompanyMaterialByCount("basicData/getCompanyMaterialNameByCount",null,"materialName");
	$("#warehouseOutTime").val(startDate);
}); 

var page = {
	bind:function(){   
		$("#materialName").on("change",function(event,data){
			var object=new Object();
			object.materialName = data;
			page.initSelect(1);
			page.getSelectVal("basicData/getModelByCountAndMaterialName",object,"model");
		});
		$("#model").bind("change",function(){
			var object=new Object(); 
			object.materialName = $("#materialName").filterSelect("getValue");
			object.model = $("#model").val();
			page.initSelect(2);
			page.getSelectVal("basicData/getColorByCountAndMaterialName",object,"color");
		});
		  
		/*$("#providerName").on("change",function(event,data){
			var object=new Object();
			object.materialName = $("#materialName").filterSelect("getValue");
			object.model = $("#model").val();
			object.providerName=data;
			page.initSelect(3); 
			page.getSelectVal('basicData/getColorByCountAndMaterialName',object,'color');
			
		});*/
		
		$("#color").bind("change",function(){
			var object=new Object();
			object.materialName = $("#materialName").filterSelect("getValue");
			object.model=$("#model").val();
			object.color=$("#color").val();
			$.ajax({
				url:'companyMaterial/getCompanyMaterialByMap',
				data:object,
				success:function(data){
					if(data.result){
						data=data.data;
						console.log("data",data)
						if(data.length>1){
							layAlert("此物料有重复");
							$("#haveCount").text('');
							$("#materialCode").val('');
							$("#colorDescription").text('');
						}else{ 
							$("#haveCount").text(data[0].count);
							$("#materialCode").val(data[0].materialCode);
							$("#colorDescription").text(data[0].colorDescription);
						}
					}
				}
			});
		});
		
		
		$("#saveBtn").bind("click",function(){
			var object=new Object();
			object.materialCode=$("#materialCode").val();
			object.count=$("#count").val();
			object.scheduledWarehouseOutTime = $("#warehouseOutTime").val();//领料时间
			object.type=5;//领料单（无投料单）
			object.remark = $("#remarks").val().trim();
			if(object.materialCode ==''){
				layAlert("物料信息不全或重复");
				return false;
			}
			if(isNaN(Number(object.count))){
				layAlert("领取数量必须是数字");
				return false;
			}
			if(Number(object.count)>0){
				if (!/^(0|[1-9]\d*)(\.\d{1,4})?$/.test(object.count)) {
					layAlert("领取数量小数点后必须是1-4位"); 
					return false;
				}
			}else{
				layAlert("领取数量必须大于0");
				return false; 
			}
			if(object.scheduledWarehouseOutTime ==''){
				layAlert("请选择时间");
				return false;
			}
			var array=new Array();
			array.push(object);
			ajaxUtil.ajaxRuquest("companyMaterial/savdOutWarehouse",{"metailArray":JSON.stringify(array)},function(data){
				layer.msg(data,{icon:1});
				page.getCompanyMaterialByCount("basicData/getCompanyMaterialNameByCount",null,"materialName");
				page.initSelect(1);
			});
		});  
	},
	getCompanyMaterialByCount:function(url,sendData,domId){
		ajaxUtil.ajaxRuquest(url,sendData,function(data){
			if(data!=null && data.length>0){
				var option = {
						data:_.map(data,function(d){
							return {
								id:d,
								name:d
							} 
						}),
						valueField:"id", 
						labelField:"name" 
				}  
				$("#"+domId).filterSelect("destroy");
				$("#"+domId).filterSelect(option);
			}
		});
	},
	getSelectVal:function(url,sendData,domId){
		ajaxUtil.ajaxRuquest(url,sendData,function(data){
			if(data!=null && data.length>0){
				$("#"+domId).html("");
				var option = "<option value=''>请选择</option>";
				for(var i=0;i<data.length;i++){   
					option+="<option value='"+data[i]+"'>"+data[i]+"</option>"
				} 
				$("#"+domId).html(option);
			}
		});
	},
	initSelect:function(index){
		var option = {
				data:[], 
				valueField:"id", 
				labelField:"name"
		} 
		var selectOption = "<option value=''>请选择</option>";
		if(index===0){  
			$("#materialName").filterSelect("destroy"); 
			$("#materialName").filterSelect(option);
			$("#model").html(selectOption); 
			$("#color").html(selectOption);
			$("#colorDescription").text("");
			$("#haveCount").text("");
			$("#materialCode").val("");
			$("#count").val("");
			$("remarks").val("");
		}
		if(index===1){
			$("#model").html(selectOption);
			$("#color").html(selectOption);
			$("#colorDescription").text("");
			$("#haveCount").text("");
			$("#materialCode").val("");
			$("#count").val("");
			$("remarks").val("");
		}
		if(index===2){ 
			$("#color").html(selectOption);
			$("#colorDescription").text("");
			$("#haveCount").text("");
			$("#materialCode").val("");
			$("#count").val("");
			$("remarks").val("");
		}
		if(index===3){ 
			$("#color").html(selectOption);
			$("#colorDescription").text("");
			$("#haveCount").text("");
			$("#materialCode").val("");
			$("#count").val("");
			$("remarks").val("");
		} 
	}
	
}